home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The PC-SIG Library 10
/
The PC-Sig Library - Shareware for the IBM PC and Compatibles (PC-SIG)(Tenth Edition Disks 1-2804)(1991).iso
/
PC_SIGCD
/
05
/
6
/
DISK0564.ZIP
/
SOURCE.ARC
/
DETAB.C
< prev
next >
Wrap
C/C++ Source or Header
|
1988-07-10
|
2KB
|
73 lines
/* this program replaces tabs in a file with the equivalent number of
spaces (tabs are assumed to be set every 8 columns). */
/* It reads from a file if a file name is specified, or from the
standard input if no file name is given. It writes to the standard
output */
/* by Jon Dart, 3012 Hawthorn St., San Diego, CA 92104 */
/* version 1.0, 16-Nov-86 */
#include <stdio.h>
#include <fcntl.h>
#define PATHSIZE 65 /* max size of DOS pathname + 1 */
#define BUFSIZE 16384 /* input buffer size */
#define EOF -1
#define TAB (unsigned char) '\011'
#define BKS (unsigned char) '\010'
#define CTRLZ (unsigned char) '\032'
extern char *malloc();
main(argc,argv)
int argc; char **argv;
{
FILE *infd;
unsigned int c2;
register int c,charpos,nexttab;
unsigned char *buffer;
buffer = (unsigned char *) malloc(BUFSIZE);
if (argc>=2) {
infd = fopen(argv[1],"r");
if (infd == NULL) {
fprintf(stderr,"\nCan't open: %s\n",argv[1]);
exit(1);
}
}
else /* read from std input */
infd = stdin;
setbuf(infd,buffer);
charpos = 1;
while ((c=getc(infd)) != EOF) {
c2 = c & 0177;
if (c2==CTRLZ)
break;
else if (c2==TAB) {
if (charpos % 8 == 0)
nexttab = charpos + 1;
else
nexttab = ((charpos / 8) + 1)*8 + 1;
while (charpos < nexttab) {
++charpos;
putc(' ',stdout);
}
}
else {
if ((c2==BKS) && (charpos>1))
--charpos;
else if (c2=='\n')
charpos = 1;
else if (c2>=32)
++charpos;
putc(c2,stdout);
}
}
if (ferror(infd)) fprintf(stderr,"detab: error in reading file\n");
if (ferror(stdout)) fprintf(stderr,"detab: error in writing file\n");
fclose(infd);
}